/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.clbs.ex.springgwt.server.dao.impl;

import com.clbs.ex.springgwt.client.model.Post;
import com.clbs.ex.springgwt.server.dao.PostDAO;
import com.clbs.ex.springgwt.server.mapper.PostMapper;
import java.util.List;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 *
 * @author peerawat.poombua
 */
public class PostDAOImpl extends JdbcDaoSupport implements PostDAO {

    public void createPost(Post post) {
        String INSERT_POST = "INSERT INTO tblPost (cMessage, dCreationDate, nIssueID)"
                + " values (?,?,?)";
        getJdbcTemplate().update(INSERT_POST
                , new Object[] {post.getMessage()
                        , post.getCreationDate()
                        , post.getIssueID()});
    }

    public Post getPost(int postID) {
        String SELECT_POST = "SELECT * FROM tblPost WHERE nPostID = ?";
        List<Post> matched = getJdbcTemplate().query(
                SELECT_POST, new Object[]{postID}, new PostMapper());
        return matched.size() > 0 ? matched.get(0) : null;
    }

    public List<Post> getPosts(int issueID) {
        String SELECT_POSTS_IN_ISSUE = "SELECT * FROM tblPost WHERE nIssueID = ?";
        List<Post> matched = getJdbcTemplate().query(
                SELECT_POSTS_IN_ISSUE, new Object[]{issueID}, new PostMapper());
        return matched.size() > 0 ? matched : null;
    }
}
